package com.digdes.calendar.gwt.client.widget;

import java.util.Date;

@SuppressWarnings("deprecation")
public class DateUtil {
	private static Date date = new Date();

	public static int getDaysInMonth(int year, int month) {
		switch (month) {
		case 1:
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				return 29; // leap year
			else
				return 28;
		case 3:
			return 30;
		case 5:
			return 30;
		case 8:
			return 30;
		case 10:
			return 30;
		default:
			return 31;
		}
	}

	public static void prevMonth() {
		int month = getMonth() - 1;
		if (month < 0) {
			setDate(getYear() - 1, 11, getDay());
		} else {
			setMonth(month);
		}
	}

	public static void nextMonth() {
		int month = getMonth() + 1;
		if (month > 11) {
			setDate(getYear() + 1, 0, getDay());
		} else {
			setMonth(month);
		}
	}

	public static void prevYear() {
		setYear(getYear() - 1);
	}

	public static void nextYear() {
		setYear(getYear() + 1);
	}

	public static void setDate(int year, int month, int day) {
		date = new Date(year - 1900, month, day);
	}

	public static void setDatePickierDate(Date d) {
		date = new Date();
		date = d;
	}

	public static int setDatePickierDay(Date d) {
		date = new Date();
		return d.getDay();
	}

	public static void setYear(int year) {
		date.setYear(year - 1900);
	}

	public static void setMonth(int month) {
		date.setMonth(month);
	}

	public static int getYear() {
		return 1900 + date.getYear();
	}

	public static int getMonth() {
		return date.getMonth();
	}

	public static int getDay() {
		return date.getDate();
	}

	public static Date getDate() {
		return date;
	}

	public static int getNowDay() {
		Date now = new Date();
		int month = getMonth();
		int year = getYear();
		int sameDay = now.getDate();
		int today = (now.getMonth() == month && now.getYear() + 1900 == year) ? sameDay
				: 0;
		return today;
	}

    public static int prevDay(){
         int day = getDay() - 1;
         setDay(day);
         return day;
    }

    public static int nextDay(){
         int day = getDay() + 1;
         setDay(day);
         return day;
    }

    public static void setDay(int day) {
        date.setDate(day);
    }

    public static String addZeroInMinutes(int minutes){
        if(minutes<10){
           return "0" + minutes;
        }
        else return String.valueOf(minutes);
    }
}
